﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Editor {
    public partial class SupplyDataEdit : Form {
        public int SupplyID = -1;
        public string sqlstr = "select * from [Supply]";
        public SupplyDataEdit() {
            InitializeComponent();
        }

        private void SupplyDataEdit_Load(object sender, EventArgs e) {
            comboBox1.SelectedIndex = 0;
            if (SupplyID != -1) {
                DataTable table = DBHelper.GetDataTable($"{sqlstr} where ID = {SupplyID}");
                textBox1.Text = table.Rows[0][0].ToString();
                textBox2.Text = table.Rows[0][1].ToString();
                comboBox1.SelectedIndex = table.Rows[0][1].ToString().Equals("男") ? 0 : 1;
                textBox3.Text = table.Rows[0][3].ToString();
                textBox4.Text = table.Rows[0][4].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            Close();
        }

        private void button2_Click(object sender, EventArgs e) {
            if (SupplyID == -1) {
                if (DBHelper.ExecuteNonQuery($"insert [Supply] values('{textBox2.Text}',{(comboBox1.SelectedItem.Equals("男") ? 1 : 0)},'{textBox3.Text}','{textBox4.Text}')")) {
                    MessageBox.Show("添加成功!");
                    Close();
                } else {
                    MessageBox.Show("添加失败!");
                }
            } else {
                if (DBHelper.ExecuteNonQuery($"update [Supply] set [Name] = '{textBox2.Text}',[Gender] = {(comboBox1.SelectedItem.Equals("男") ? 1 : 0)},[Address] = '{textBox3.Text}',[Tel] = '{textBox4.Text}' where ID = {textBox1.Text}")) {
                    MessageBox.Show("添加成功!");
                    Close();
                } else {
                    MessageBox.Show("添加失败!");
                }
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
